package Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Vstar
 */
public class Xparser {

        //String displayStrings[] = new String[1000];
        String infNode = "";
        String thumb = "";
        String returnedString = "";
        int numberDisplayLines = 0;
        HttpServletResponse response = null;

        public Xparser(HttpServletResponse response) {
            this.response = response;
        }

        public String displayDocument(String uri) {
            try {

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                DocumentBuilder db = null;
                try {
                    db = dbf.newDocumentBuilder();
                } catch (ParserConfigurationException pce) {
                }

                Document document = null;
                document = db.parse(uri);

                numberDisplayLines = 0;
                display(document, "");

            } catch (Exception e) {
                e.printStackTrace(System.err);
            }

            return this.returnedString;
        }

        public void display(Node node, String indent) {
            if (node == null) {
                return;
            }

            int type = node.getNodeType();
            String value = node.getNodeName();

            switch (type) {
                case Node.DOCUMENT_NODE: {
                    display(((Document) node).getDocumentElement(), "");
                    break;
                }

                case Node.ELEMENT_NODE: {
                    int length = (node.getAttributes() != null) ? node.getAttributes().getLength() : 0;

                    NodeList childNodes = node.getChildNodes();
                    if (childNodes != null) {
                        length = childNodes.getLength();
                        indent += "&nbsp;&nbsp;&nbsp;&nbsp;";
                        for (int i = 0; i < length; i++) {
                            display(childNodes.item(i), indent);
                        }
                    }
                    break;
                }

                case Node.CDATA_SECTION_NODE: {
                    break;
                }

                case Node.TEXT_NODE: {
                    String newText = node.getNodeValue().trim();
                    if (newText.indexOf("\n") < 0 && newText.length() > 0) {
                    }
                    break;
                }

                case Node.PROCESSING_INSTRUCTION_NODE: {
                    break;
                }
            }
            if (type == Node.ELEMENT_NODE &&  "Model".equalsIgnoreCase(value)) {

                String Id = node.getAttributes().getNamedItem("id").getNodeValue();
                String Thumb = node.getAttributes().getNamedItem("thumbnail").getNodeValue();

                 String s = "<li><a rel=\"f\" title=\"asdf\" onclick=\"navigate('" + Id +"')\">"
                        + "<img src=\"" + Thumb + "\" width=\"70\" height=\"100\" alt=\"FF\"/>"
                        + "</a>"
                        + "</li>";

                this.returnedString = this.returnedString + s;
                try {
                    //   this.response.getWriter().write(s);
                } catch (Exception d) {
                }
            }
        }

        /** Returns element value
         * @param elem element (it is XML tag)
         * @return Element value otherwise empty String
         */
        public String getElementValue(Node elem) {
            Node kid;
            if (elem != null) {
                if (elem.hasChildNodes()) {
                    for (kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
                        if (kid.getNodeType() == Node.TEXT_NODE) {
                            System.out.println("kid: " + kid.getNodeValue());
                            return kid.getNodeValue();
                        }
                    }
                }
            }
            return "";
        }
        //XMl Node reader - Vietnd
        String flag;

        public String getNode(String url, String id) {

            try {
                //"D:\\FPT University\\Develop\\ModelPhotoGallery_v3\\build\\web\\WEB-INF\\MyXMLFile.xml"
                File file = new File(url);
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(file);
                doc.getDocumentElement().normalize();
                System.out.println("Root element " + doc.getDocumentElement().getNodeName());
                NodeList nodeLst = doc.getElementsByTagName("Model");

                //System.out.println(doc.getElementsByTagName("Model") );
                System.out.println("Information of all Model");

                for (int s = 0; s < nodeLst.getLength(); s++) {

                    Node fstNode = nodeLst.item(s);
                    //System.out.println("Node value: " + getElementValue(fstNode));

                    NamedNodeMap attributes = fstNode.getAttributes();
                    //for (int i = 0; i < attributes.getLength(); i++) {
                    Node attribute = attributes.item(1);
                    System.out.println("AttributeName: " + attribute.getNodeName() + ", attributeValue: " + attribute.getNodeValue());
                    flag = attribute.getNodeValue();
                    //}
                    if (id.equalsIgnoreCase(flag)) {
                        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                            Element elmnt_1 = (Element) fstNode;
                            NodeList elmntLst_1 = elmnt_1.getElementsByTagName("Name");
                            Element secondElmnt_1 = (Element) elmntLst_1.item(0);
                            NodeList Nls_1 = secondElmnt_1.getChildNodes();
                            System.out.println("Name : " + ((Node) Nls_1.item(0)).getNodeValue());
                            infNode += "Name : <strong>" + ((Node) Nls_1.item(0)).getNodeValue() + "</strong><br>";

                            Element elmnt_2 = (Element) fstNode;
                            NodeList elmntLst_2 = elmnt_2.getElementsByTagName("BirthDate");
                            Element secondElmnt_2 = (Element) elmntLst_2.item(0);
                            NodeList Nls_2 = secondElmnt_2.getChildNodes();
                            System.out.println("BirthDate : " + ((Node) Nls_2.item(0)).getNodeValue());
                            infNode += "BirthDate : " + ((Node) Nls_2.item(0)).getNodeValue() + "<br>";

                            Element elmnt_3 = (Element) fstNode;
                            NodeList elmntLst_3 = elmnt_3.getElementsByTagName("Measurement");
                            Element secondElmnt_3 = (Element) elmntLst_3.item(0);
                            NodeList Nls_3 = secondElmnt_3.getChildNodes();
                            System.out.println("Measurement : " + ((Node) Nls_3.item(0)).getNodeValue());
                            infNode += "Measurement : " + ((Node) Nls_3.item(0)).getNodeValue() + "<br>";

                            Element elmnt_4 = (Element) fstNode;
                            NodeList elmntLst_4 = elmnt_4.getElementsByTagName("Height");
                            Element secondElmnt_4 = (Element) elmntLst_4.item(0);
                            NodeList Nls_4 = secondElmnt_4.getChildNodes();
                            System.out.println("Height : " + ((Node) Nls_4.item(0)).getNodeValue());
                            infNode += "Height : " + ((Node) Nls_4.item(0)).getNodeValue() + "<br>";

                            Element elmnt_5 = (Element) fstNode;
                            NodeList elmntLst_5 = elmnt_5.getElementsByTagName("Weight");
                            Element secondElmnt_5 = (Element) elmntLst_5.item(0);
                            NodeList Nls_5 = secondElmnt_5.getChildNodes();
                            System.out.println("Weight : " + ((Node) Nls_5.item(0)).getNodeValue());
                            infNode += "Weight : " + ((Node) Nls_5.item(0)).getNodeValue() + "<br>";

                            Element elmnt_6 = (Element) fstNode;
                            NodeList elmntLst_6 = elmnt_6.getElementsByTagName("Education");
                            Element secondElmnt_6 = (Element) elmntLst_6.item(0);
                            NodeList Nls_6 = secondElmnt_6.getChildNodes();
                            System.out.println("Education : " + ((Node) Nls_6.item(0)).getNodeValue());
                            infNode += "Education : " + ((Node) Nls_6.item(0)).getNodeValue() + "<br>";

                            Element elmnt_7 = (Element) fstNode;
                            NodeList elmntLst_7 = elmnt_7.getElementsByTagName("Hobby");
                            Element secondElmnt_7 = (Element) elmntLst_7.item(0);
                            NodeList Nls_7 = secondElmnt_7.getChildNodes();
                            System.out.println("Hobby : " + ((Node) Nls_7.item(0)).getNodeValue());
                            infNode += "Hobby : " + ((Node) Nls_7.item(0)).getNodeValue() + "<br>";

                            Element elmnt_8 = (Element) fstNode;
                            NodeList elmntLst_8 = elmnt_8.getElementsByTagName("Hate");
                            Element secondElmnt_8 = (Element) elmntLst_8.item(0);
                            NodeList Nls_8 = secondElmnt_8.getChildNodes();
                            System.out.println("Hate : " + ((Node) Nls_8.item(0)).getNodeValue());
                            infNode += "Hate : " + ((Node) Nls_8.item(0)).getNodeValue() + "<br>";
                        }

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return infNode;
        }

        public String getThumnail(String url, String id) {

            try {
                //"D:\\FPT University\\Develop\\ModelPhotoGallery_v3\\build\\web\\WEB-INF\\MyXMLFile.xml"
                File file = new File(url);
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(file);
                doc.getDocumentElement().normalize();
                System.out.println("Root element " + doc.getDocumentElement().getNodeName());
                NodeList nodeLst = doc.getElementsByTagName("Model");

                //System.out.println(doc.getElementsByTagName("Model") );
                System.out.println("Information of all Model");

                for (int s = 0; s < nodeLst.getLength(); s++) {

                    Node fstNode = nodeLst.item(s);
                    //System.out.println("Node value: " + getElementValue(fstNode));

                    NamedNodeMap attributes = fstNode.getAttributes();
                    //for (int i = 0; i < attributes.getLength(); i++) {
                    Node attribute = attributes.item(1);
                    System.out.println("AttributeName: " + attribute.getNodeName() + ", attributeValue: " + attribute.getNodeValue());
                    Node attribute2 = attributes.item(2);
                    System.out.println("AttributeName2: " + attribute2.getNodeName() + ", attributeValue2: " + attribute2.getNodeValue());

                    flag = attribute.getNodeValue();
                    //}
                    if (id.equalsIgnoreCase(flag)) {
                        thumb = attribute2.getNodeValue();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return thumb;
        }

}
